**************************
**************************
* Graphs
**************************
**************************

use "${FINALDATA}\immo.dta", clear

** save graphs to GRAPHS folder:
cd "$GRAPHS\"

** drop observations from last month (unclear when sold)
drop if monat == monthly("2015 10","YM") 
 


****************
* Figure 2: Adoption rates of EPCs, by seller type
****************

local savename xsection_epc_selltype
preserve
	keep if inrange(monat, monthly("2013 01","YM"), monthly("2015 10","YM"))
	bysort monat: egen mw_epc = mean(epc == 1)
	foreach var of numlist 2 4 5 {
		bysort monat: egen mw_epc_`var' = mean(epc == 1) if anbietertyp==`var'
		}
	collapse (mean) mw_epc*, by(monat)
	local xlin = monthly("2014 05","YM")
	*local xlin2 = monthly("2015 05","YM")-0.5
	twoway (scatter mw_epc monat, color(gs6) xline(`xlin') /* xline(`xlin2')*/ ) ///
			(line mw_epc_2 monat, lcolor(navy)) ///
			(line mw_epc_4 monat,  lcolor(forest_green) lpattern(shortdash)) ///
			(line mw_epc_5 monat, lcolor(dkorange) lpattern(longdash))  ///
			/// (line mw_epc_7 monat ) /// 
			, xtitle("")  ytitle("Share of ads with energy information") tlabel(2013m1(4)2015m9, format(%tmMonYY)) ///
				legend( label(1 "Total share")  label(2 "Banks")  label(3 "Estate agents") label(4 "Private sellers") /* label(5 "Housing industry") */) ///
				xmtick(##6) ///
				graphregion(color(white)) ///
				saving("`savename'", replace)
	graph export "`savename'.pdf", replace			
restore


************************************************
** Figure 3: Distribution of Heat Energy Consumption per Square Meter
************************************************

local savename xsection_distribution_evk_pre_post
twoway (kdensity evk if postmai == 0) /// 
		(kdensity evk if postmai == 1, lpattern(shortdash)) ///
		, legend(label(1 "before May 1, 2014") label(2 "After April 30, 2014")) xtitle("Energy consumption, in kWh per m{sup:2} and year") ytitle(Estimated density) ///
		graphregion(color(white)) ///
		saving("`savename'", replace)
graph export "`savename'.pdf", replace


****************
* Figure 5 (Appendix F): Price Evolution by Seller Types
****************

global merkat baujahr_adopt modern_adopt objektkat qualitaet heizart
global merkat_i ""
foreach x of global merkat {
	global merkat_i $merkat_i i.`x' 
}
global merkont grundflaeche wohnflaeche zimmer_cont grundflaeche2 wohnflaeche2 


preserve

replace zimmer_cont = . if zimmer == .
gen zimmer_cont2 = zimmer_cont^2

gen baujahr_adopt = baujahr_kat
recode baujahr_adopt ( 1930 1950 1960 1970 = 1900) (1995 1990 = 1977) (2014 2008 = 2002)
gen modern_adopt = modern_kat
recode modern_adopt (1990 = 1977) (2015 2008 = 2002)

* Estimation to eliminate influence of observables
xtset plz
xtreg logkaufp $merkat_i $merkont ib1.objektzus ib5.anbietertyp, fe
cap drop resid
predict resid, e

** Plot graph and save
local savename xsection_price_selltype_resid

	foreach var of numlist 2 4 5 {
		bysort monat: egen mean_price_`var' = mean(resid) if anbietertyp==`var' 
	}
	bysort monat: egen mean_price_0 = mean(resid)
	collapse (mean) mean_price_*, by(monat)
	local xlin = monthly("2014 05","YM")-0.5
	twoway 	(line mean_price_2 monat, xline(`xlin') xline(`xlin2') ) ///
			(line mean_price_4 monat, lpattern(shortdash)) ///
			(line mean_price_5 monat, lpattern(longdash)) ///
			, xtitle("") ytitle("Average residual price, in logs") xlabel(#6, format(%tmMonYY)) legend(/*label(1 "Total")*/ label(1 "Banks")  label(2 "Estate agents") label(3 "Private sellers") /* label(4 "Housing industry")*/ ) ///
				xmtick(##5) ///
				graphregion(color(white)) ///
				saving("`savename'", replace)
	graph export "`savename'.pdf", replace	
restore

